﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aerthe.Model.Tasks
{
    public class MTask5
    {
        public List<ListsItem> ListTask = new List<ListsItem> { };
        string BuforString;
        double BuforDouble;

        public MTask5()
        {
            using (var Base = new NorthwindEntities())
            {
                var ListCustomers = Base.Orders.Select(m => new 
                {
                    InfoCustomers = m.Customers.CompanyName,
                    ValuePurchases = m.Order_Details.Sum(n => n.UnitPrice * n.Quantity)
                }).OrderBy(n => n.ValuePurchases).ToList();

                foreach (var a in ListCustomers)
                {
                    this.ListTask.Add(new ListsItem(a.InfoCustomers,  Convert.ToDouble(a.ValuePurchases).ToString()));
                }
            }
        }

        public void MViewConsole()
        {
            BuforString = "Normal";

            Console.WriteLine("|  {0,-35} |  {1} |", "Name Company", "Value Purchases");

            foreach (var a in ListTask)
            {
                BuforDouble = Convert.ToDouble(a.Item2);

                if (BuforDouble > 100000)
                {
                    if (BuforString != "Gold")
                    {
                        BuforString = "Gold";
                        Console.WriteLine("|  {0,-35} |", BuforString);
                    }
                }
                else if (BuforDouble > 10000)
                {
                    if (BuforString != "Silver")
                    {
                        BuforString = "Silver";
                        Console.WriteLine("|  {0,-35} |", BuforString);
                    }
                }
                else
                {
                    if (BuforString != "Normal")
                    {
                        BuforString = "Normal";
                        Console.WriteLine("|  {0,-35} |", BuforString);
                    }
                }

                Console.WriteLine("|  {0,-35} |  {1} USD", a.Item1, a.Item2);
            }
        }
    }
}
